# coding=UTF-8
from django.contrib.syndication.feeds import Feed, FeedDoesNotExist
from blog.models import Post
from django.utils.encoding import force_unicode,smart_str
import urllib

class LatestEntries(Feed):
    title="LatestEntries"
    link="/post/"
    description="Updates on changes and additions to iHere"
    
    def item_link(self, obj):
        if not obj:
            raise FeedDoesNotExist
        return '/post/'+smart_str(obj.key().id())
    
    def items(self,obj):
        return Post.all().order('-date')[:5]
    
class LatestEntriesByCategory(Feed):
    title="LatestEntriesByCategory"
    link="/post/"
    description="Updates on changes and additions to iHere"
    
    def get_object(self, bits):
        if len(bits)!=1:
            return Post.all().order('-date')[:5]
        return Post.all().filter('catalog =',force_unicode(urllib.unquote(smart_str(bits[0])))).order('-date')[:5]
    
    def item_link(self, obj):
        if not obj:
            raise FeedDoesNotExist
        return '/post/'+smart_str(obj.key().id())
    
    def items(self,obj):
        return obj
    
class PostEntry(Feed):
    title="iHere's PostEntry"
    link="/post/"
    description="iHere's Blog"
    
    def get_object(self, bits):
        if len(bits)!=1:
            raise FeedDoesNotExist
        return [Post.get_by_id(int(force_unicode(urllib.unquote(smart_str(bits[0]))))),]
        
    
    def item_link(self, obj):
        if not obj:
            raise FeedDoesNotExist
        return '/post/'+smart_str(obj.key().id())
    
    def items(self,obj):
        return obj